<?php
/**
 * Yotaku shop - yshop
 * Copyright (c) 2016 http://www.yotaku.cn All rights reserved.
 * Author: Creatint <creatint@163.com> <http://creatint.yotaku.cn>
 */
namespace Addons\Second\Model;
use Think\Model;

class SecondModel extends Model{
    //定义表明
    protected $tableName = 'activity_second';

    protected $error = '';

    //自动验证
    protected $_validate = array(
        array('start','require','起始时间不能为空',0),
        array('number','require','起始时间不能为空',0),
        array('price','require','起始时间不能为空',0),
    );



    function getDetail($goods_id, $field){
        if((int)$goods_id){
            $map = array('goods_id'=>(int)$goods_id);
        }else{
            $this->error = '不存在商品ID';
            return false;
        }
        $res = $this->where($map)->field($field)->find();
        if(!empty($res)){
            //Redis 获取number
            $res['start_number'] = $res['number'];
            $res['number'] = $this->redis($goods_id);

        }
        return $res;
    }

    /**
     * @describe 获取商品活动数据，若number=0，则商品售罄
     * @author Creatint
     * @param $goods_id
     * @return bool|mixed
     */
    function getItem($goods_id, $activity=0){
//        echo '初始redis库存 ';dump($this->redis($goods_id));
        trace($this->redis($goods_id),'Redis库存');
        //检查是否参与活动
        if(0 == $activity){
            $this->error('非活动商品');
            return false;
        }

        //检查在活动表中是否存在
        if((int)$goods_id){
            $map_activity = array();
            $map_activity['goods_id'] = (int)$goods_id;
        }else{
            $this->error = '不存在商品ID';
            return false;
        }
        $res_activity = $this->where($map_activity)->find();
        if(!$res_activity){
            //不存在活动数据
            $this->error = '不存在活动数据';
            return false;

        }else{
            //存在活动数据
            //检查是否存在购物车
            $map_cart = array();
            $map_cart['goods_id'] = $goods_id;
            $map_cart['activity'] = $activity;
            $map_cart['user_id'] = session("member.id");

            //以活动购物车商品数量为依据
            if($cart = M("Cart")->where($map_cart)->find()){
                //存在购物车 若购物车数量为0，则减少Redis库存，否则不减少
                if( 0 == $cart['number'] ){
                    //Redis库存判断
                    $number = $this->redis((int)$goods_id);
                    if($number>=1){
                        $this->redis((int)$goods_id,($number-1));
                        $res_activity['number'] = 1;
                        return $res_activity;
                    }else{
                        //售罄
                        $this->error = '售罄';
                        return false;
                    }
                }else{
                    //数量不为0，则不减少库存
                    return array('number'=>$cart['number'] , 'price'=>$res_activity['price']);
                }
            }else{
                //不存在，则直接建超库存
                $number = $this->redis((int)$goods_id);
                if($number>=1){
                    $this->redis((int)$goods_id,($number-1));
                    $res_activity['number'] = 1;
                    return $res_activity;
                }else{
                    //售罄
                    $this->error = '售罄';
                    return false;
                }
            }

        }


/*


        //查看当前商品是否已有临时购物车
        $map_current = array();
        $map_current['goods_id'] = $goods_id;
        $map_current['activity'] = $activity;
        $map_current['status'] = 2;

        $res_current = $this->where($map_current)->find();
        if($res_current){
            //存在临时购物车，数据仍然以redis为准
            $number = $this->redis($goods_id);
            //REDIS是否可以购买
            if($number>=1){
                $this->redis($goods_id,($number-1));
                $res_current['number'] = 1;
                //TODO:用户获得一个商品，记录用户对其的够买权限
            }else{
                $res['number'] = 0;
            }

        }else{
            //不存在临时购物车，获取活动数据表
            $res_activity = $this->where($map_activity)->find();

            if($res_activity){
                //存在活动,对redis减少库存
                $number = $this->redis($goods_id);


            }else{
                //不存在活动，也没有临时购物车，因此无够买权限
                return false;
            }
        }

*/
        /*if($res){
            //Redis 获取number
            //$res['number'] = $this->redis($res['goods_id']);


            //查看是否已有购买资格的缓存  goods_id=>number
            $tmp_goods = session('activity_second_goods');
            if(!empty($tmp_goods) && !empty($tmp_goods[$goods_id])){
                //存在缓存
                $number = $tmp_goods[$goods_id];
                //TODO:number应该是1
                $res['number'] = 1;
            }else{
                //不存在缓存
                $number = $this->redis($goods_id);
                if($number>=1){
                    $this->redis($goods_id,($number-1));
                    $res['number'] = 1;
                    //TODO:用户获得一个商品，记录用户对其的够买权限
                    $tmp_goods[$goods_id] = 1;
                    session('activity_second_goods',$tmp_goods);
                }else{
                    $res['number'] = 0;
                }
            }
            $res['status'] = 0;
            //还未开始发售
            if($res['start']>=time()){
                $res['status'] = 1;
            }
            $res['current'] = time();
            //商品活动过期
            if($res['end']<= time()){
                $res['status'] = 2;
            }


        }*/
//        return $res;
    }



    function update($data){
        if($data['goods_id']>0){
            //存在ID，更新

            $map = array('goods_id'=>$data['goods_id']);
            if($this->where($map)->count('id')>0){
                if(($data = $this->create($data)) && $this->where($map)->save($data)){

                    //Redis存储number
                    $this->redis($data['goods_id'],$data['number']);
                    return $data;
                }else{
                    $this->error = '修改失败';
                    return $data;
                }
            }else{
                if(($data = $this->create($data)) && ($id = $this->add($data))){
                    $data['id'] = $id;
                    return $data;
                }else{
                    $this->error = '新增失败';
                    return false;
                }
            }

        }else{
            //不存在商品ID
            $this->error = '不存在商品ID';
            return false;
        }
    }

    public function del($id=null, $goods_id=null){
        if($goods_id>0){
            $map = array('goods_id'=>$goods_id);
        }elseif($id>0){
            $map = array('id'=>$id);
        }
        $this->where($map)->delete();
    }

    public function redis($key=null, $value=-1){
        $key = "{activity_second_goods_id_$key}";
        if($_GET['redis'] != 'no'){
            Vendor("RedisExtend.RedisExtend");
            $redis = new \RedisExtend();
            if(!$redis->connect(array('host'=>'127.0.0.1','port'=>6379))){
                return false;
            }
            if(null == $value){
                $redis->remove($key);
            }elseif(-1 == $value){
                $number = $redis->get($key);
                return $number?$number:0;
            }else{
                $redis->set($key,$value);
            }
        }

    }

    public function getError(){
        return $this->error;
    }
}
